package org.csu.tank.protocol;

import javafx.application.Platform;
import org.csu.tank.GameConfig;
import org.csu.tank.client.TankClient;
import org.csu.tank.components.Dir;
import org.csu.tank.components.PlayerComponent;

import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;

/**
 * 坦克出生消息协议
 */
public class TankNewMsg implements Msg {
      private int msgType = Msg.TANK_NEW_MSG;
      private TankClient tc;

      private PlayerComponent tankPlayer;

      public TankNewMsg(PlayerComponent playerComponent) {
            this.tankPlayer = playerComponent;
      }

      public TankNewMsg(TankClient tc) {
            this.tc = tc;
            this.tankPlayer = tc.getPlayerComponent();
      }

      public void send(DatagramSocket ds, String IP, int UDP_Port) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream(88);
            DataOutputStream dos = new DataOutputStream(baos);
            try {
                  dos.writeInt(msgType);
                  dos.writeInt(tankPlayer.getId());
                  dos.writeDouble(tankPlayer.getX());
                  dos.writeDouble(tankPlayer.getY());
                  dos.writeInt(tankPlayer.getMoveDir().ordinal());
                  dos.writeBoolean(tankPlayer.isGood());
//                  dos.writeUTF(tankPlayer.getName());
            } catch (IOException e) {
                  e.printStackTrace();
            }
            byte[] buf = baos.toByteArray();
            try {
                  DatagramPacket dp = new DatagramPacket(buf, buf.length, new InetSocketAddress(IP, UDP_Port));
                  ds.send(dp);
            } catch (IOException e) {
                  e.printStackTrace();
            }
      }

      public void parse(DataInputStream dis) {
            try {
                  int id = dis.readInt();
                  if (id == this.tankPlayer.getId()) {
                        return;
                  }
                  boolean exist = false;
                  double x = dis.readDouble();
                  double y = dis.readDouble();
                  Dir dir = Dir.values()[dis.readInt()];
                  boolean good = dis.readBoolean();
//                  String tankName = dis.readUTF();

                  System.out.println("接受到消息！！");

                  //TODO 遍历坦克队列检测是否已经存在
                  for (PlayerComponent tankPlayer : tc.getPlayerComponentList()) {
                        if (id == tankPlayer.getId()) {
                              exist = true;
                              break;
                        }
                  }

                  if (!exist) {

                        Platform.runLater(new Runnable() {
                              @Override
                              public void run() {

                                    //TODO 遍历坦克队列检测是否已经存在
                                    boolean exist2 = false;
                                    for (PlayerComponent tankPlayer : tc.getPlayerComponentList()) {
                                          if (id == tankPlayer.getId()) {
                                                exist2 = true;
                                                break;
                                          }
                                    }
                                    if (!exist2) {
                                          tc.creatNewPlayer(id, GameConfig.PLAYER_HEALTH, x, y, dir, good);
                                          System.out.println("new msg生成坦克:" + id);
                                    }
                              }
                        });

//                  Platform.runLater(new Runnable() {
//                        @Override
//                        public void run() {
//                              tc.creatNewPlayer(1,9*12+3,25*6,Dir.DOWN,true);
//                        }
//                  });

                        TankAlreadyExistMsg msg = new TankAlreadyExistMsg(tankPlayer);
                        tc.getNc().send(msg);
                  }
            } catch (IOException e) {
                  System.out.println("未收到消息！！");
                  e.printStackTrace();
            }
      }
}
